// subPages/pensionService/rehabilitatioEducation/rehabilitationEducation.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = 10,type=''
Page({
  /**
   * 页面的初始数据
   */
  data: {
    themeColor:app.globalData.themeColor,
    communityId:app.globalData.communityId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.type){
        that.setData({
            maintype:options.type
        })
        var url = ''
        var title = ''
        switch (that.data.maintype) {
            case '1':
                url = 'get_community_health_new_video_all_copy'
                title = '康复教育'
                type = 'education'
                break;
        
            case '2':
                url = 'get_community_health_teacher_video_copy'
                title = '线上指导'
                type = 'lecture'
                break;
        }
        that.setData({
            typeUrl: url
        })
        loadMoreView = that.selectComponent("#loadMoreView")
        util.setNavBarTitle(title)
        util.setNavBarColor('#ffffff',this.data.themeColor);
    }
    
  },

    //获取内容
    getContentAll:function(){
        var data = [];
        data.push("0");
        data.push(this.data.communityId);
        data = JSON.stringify(data);
        //获取数据数据
        util.getAjax(`${app.globalData.apiUrl}/${this.data.typeUrl}`,this.getContent,{p: p,num: num,community_id: data},this.contentErr)
    },
    
    //获取健康资料内容
    getContent(res){
        // console.log(res)
        var that = this
        if(res.data.length != 0){
            var contentList = []
            for(var i in res.data.list){
                contentList.push(res.data.list[i])
            }
            that.setData({
                isContent: true,
                contentList: contentList
            })
        }else{
            that.setData({
                isContent: false,
                contentList: []
            })
        }
        loadMoreView.loadMoreComplete(res.data,type)
    },

    //获取商品数据异常
    contentErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            var text = ''
            switch (this.data.maintype) {
                case '1':
                    text = '康复教育'
                    break;
                case '2':
                    text = '线上指导'
                    break;
            }
            util.showModal('提示','获取' + text + '失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },

    loadMoreListener: function(e) {
        p += 1
        this.getContentAll()
    },

    clickLoadMore: function(e) {
        this.getContentAll()
    },

    //内容详情
    contentBtn:function(e){
        if(e.currentTarget.dataset.type == 1){
            wx.navigateTo({
                url: '/subPages/pensionService/articleContent/articleContent?id=' + e.currentTarget.dataset.id,
            })
        }
    },

    //播放结束
    viedoEnd:function(e){
        var that = this
        var type = that.data.maintype == 1?5:6
        util.getAjax(app.globalData.apiUrl +'/add_see',function(){
            //获取健康资料内容
            that.getContentAll()
        },{id: e.currentTarget.dataset.id,type: type})
    },

    //回到顶部
    goTop: function (e) {
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },

    // 获取滚动条当前位置
    onPageScroll: function (e) {
        // console.log(e)
        if (e.scrollTop > 600) {
            this.setData({
                isTop: true
            });
        } else {
            this.setData({
                isTop: false
            });
        }
    },

    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(that.data.typeUrl){
      if(app.globalData.communityId){
        that.setData({
          communityId: app.globalData.communityId
        })
        that.getContentAll()
      }else{
        app.communityIdCallback = communityId => {
          if (communityId != '') {
            that.setData({
              communityId: communityId
            })
            that.getContentAll()
          }
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
        this.getContentAll()
        return
    }
    loadMoreView.loadMore()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
